<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
</head>

<body>
	<script type="text/javascript">
		//1.存储:cookie在浏览器里,以key=value;最好单个存储。如果有多个会自动给你加"; "隔开    
		//最好单个存储
		document.cookie = "e=7";
		document.cookie = "f=10";
		console.log(document.cookie) //读取的是字符串。注意有空格  "e=7; f=10" 

		//2.1 读取单个cookie-根据传入键读取值
		// 已知str="e=7; f=8" 
		/* 	var arr=document.cookie.split("; ");
		  for (var i = 0; i < arr.length; i++) {
		  var arr2=arr[i].split('=');
		  // console.log(arr2[0],arr2[1])
		  if(arr2[0]=='e'){
			  console.log(arr2[1])
		  }		  
	} */

		//2.2 封装-读取单个cookie-根据已知找对应的
		function getCookie(name) {
			var arr = document.cookie.split("; ");
			for (var i = 0; i < arr.length; i++) {
				var arr2 = arr[i].split('=');
				// console.log(arr2[0],arr2[1])
				if (arr2[0] == name) {
					return arr2[1]
				}
			}
			return '' //容错处理-如果用户传入的值没有,返回空.否则会出现undefined

		}
		var value = getCookie('f')
		document.write('欢迎用户' + value)


	</script>
</body>

</html>